package com.csair.platform.cit.service.biz.impl;

import cn.hutool.core.lang.Assert;
import com.csair.invoice.common.base.util.collection.CollectionUtil;
import com.csair.platform.cit.facade.model.citwgqyGrid.sy.vo.*;
import com.csair.platform.cit.service.biz.ICitWgqyGridService;
import com.csair.platform.cit.service.dao.a200000.*;
import com.csair.platform.cit.service.entity.*;
import com.csair.platform.cit.service.util.JsonHelper;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @author zhengxincheng
 */
@Slf4j
@Service
public class CitWgqyGridServiceImpl implements ICitWgqyGridService {

    @Autowired
    CitWgqyGridRepository fb;

    @Autowired
    CitWgqyGridFb1Repository fb1;

    @Autowired
    CitWgqyGridFb2Repository fb2;

    @Autowired
    CitWgqyGridFb3Repository fb3;

    @Autowired
    CitWgqyGridFb4Repository fb4;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(CitWgqyGridItemVo citWgqyGridItemVo) {

        Assert.isTrue(StringUtils.isNotEmpty(citWgqyGridItemVo.getNsrsbh()),"纳税人不能是空值");
        Assert.isTrue(StringUtils.isNotEmpty(citWgqyGridItemVo.getSkssqq()),"税款所属日期起不能空值");
        Assert.isTrue(StringUtils.isNotEmpty(citWgqyGridItemVo.getSkssqz()),"税款所属日期止不能空值");

        CitWgqyGrid tmepObj = null;

        if(!Objects.isNull(citWgqyGridItemVo.getId())){
            tmepObj = fb.queryById(citWgqyGridItemVo.getId());
            deleteById(citWgqyGridItemVo.getId());
        }

        CitWgqyGrid fbObj = (CitWgqyGrid) JsonHelper.from(citWgqyGridItemVo.getCitWgqyGrid(),CitWgqyGrid.class);

        if(!Objects.isNull(tmepObj)){
            fbObj.setNsrmc(fbObj.getNsrmc());
            fbObj.setCreateTime(tmepObj.getCreateTime());
        }else{
            fbObj.setCreateTime(new Date());
        }

        fbObj = fb.save(fbObj);

        citWgqyGridItemVo.setParentId(fbObj.getId());

        List<CitWgqyGridFb1> fb1List = JsonHelper.from(citWgqyGridItemVo.getCitWgqyGridFb1(), new TypeToken<List<CitWgqyGridFb1>>() {
        }.getType());

        List<CitWgqyGridFb2> fb2List = JsonHelper.from(citWgqyGridItemVo.getCitWgqyGridFb2(), new TypeToken<List<CitWgqyGridFb2>>() {
        }.getType());

        List<CitWgqyGridFb3> fb3List = JsonHelper.from(citWgqyGridItemVo.getCitWgqyGridFb3(), new TypeToken<List<CitWgqyGridFb3>>() {
        }.getType());

        List<CitWgqyGridFb4> fb4List = JsonHelper.from(citWgqyGridItemVo.getCitWgqyGridFb4(), new TypeToken<List<CitWgqyGridFb4>>() {
        }.getType());

        if(CollectionUtil.isNotEmpty(fb1List)){
            fb1.saveAll(fb1List);
        }
        if(CollectionUtil.isNotEmpty(fb2List)){
            fb2.saveAll(fb2List);
        }
        if(CollectionUtil.isNotEmpty(fb3List)){
            fb3.saveAll(fb3List);
        }
        if(CollectionUtil.isNotEmpty(fb4List)){
            fb4.saveAll(fb4List);
        }
    }

    @Override
    public Page<CitWgqyGrid> query(CitWgqyGridRequest params) {
        //Assertions.assertThatThrownBy()
        Assert.isTrue(StringUtils.isNotEmpty(params.getNsrsbh()),"纳税人不能是空值");
        Assert.isTrue(StringUtils.isNotEmpty(params.getSkssqq()),"税款所属日期起不能空值");
        Assert.isTrue(StringUtils.isNotEmpty(params.getSkssqz()),"税款所属日期止不能空值");
        Assert.isTrue(params.getPageIndex() > 0,"页数不能小于0");

        params.setPageIndex(Optional.ofNullable(params.getPageIndex()).orElse(1));
        params.setPageSize(Optional.ofNullable(params.getPageSize()).orElse(10));
        Pageable pageable = PageRequest.of(params.getPageIndex() - 1 ,params.getPageSize());
        return fb.queryByNsrsbhAndSkssqqAndSkssqzOrderByCreateTime(params.getNsrsbh(),params.getSkssqq(),params.getSkssqz(),pageable);
    }

    @Override
    public CitWgqyGridItemVo queryById(Long id){

        CitWgqyGridItemVo vo = new CitWgqyGridItemVo();

        Assert.isTrue(!Objects.isNull(id),"id不能是空值");

        CitWgqyGridVo fbObj = (CitWgqyGridVo) JsonHelper.from(fb.queryById(id),CitWgqyGridVo.class);
        if(Objects.isNull(fbObj)){
            return vo;
        }
        List<CitWgqyGridFb1Vo> fb1List = JsonHelper.from(fb1.queryByParentId(id), new TypeToken<List<CitWgqyGridFb1Vo>>() {
        }.getType());

        List<CitWgqyGridFb2Vo> fb2List = JsonHelper.from(fb2.queryByParentId(id), new TypeToken<List<CitWgqyGridFb2Vo>>() {
        }.getType());

        List<CitWgqyGridFb3Vo> fb3List = JsonHelper.from(fb3.queryByParentId(id), new TypeToken<List<CitWgqyGridFb3Vo>>() {
        }.getType());

        List<CitWgqyGridFb4Vo> fb4List = JsonHelper.from(fb4.queryByParentId(id), new TypeToken<List<CitWgqyGridFb4Vo>>() {
        }.getType());

        vo.setCitWgqyGrid(fbObj);
        vo.setCitWgqyGridFb1(fb1List);
        vo.setCitWgqyGridFb2(fb2List);
        vo.setCitWgqyGridFb3(fb3List);
        vo.setCitWgqyGridFb4(fb4List);
        vo.setId(fbObj.getId());
        vo.setNsrsbh(fbObj.getNsrsbh());
        vo.setSkssqq(fbObj.getSkssqq());
        vo.setSkssqz(fbObj.getSkssqz());
        return vo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(Long id) {
        Assert.isTrue(!Objects.isNull(id),"主键id不能是空值");
        fb.deleteById(id);
        fb1.deleteByParentId(id);
        fb2.deleteByParentId(id);
        fb3.deleteByParentId(id);
        fb4.deleteByParentId(id);
    }
    @Override
    public List<CitWgqyGrid> getCitWgqyGrid(String nsrsbh,String skssqq,String skssqz){
        StringBuffer sb = new StringBuffer();
        CitWgqyGrid query = new CitWgqyGrid();
        query.setNsrsbh(nsrsbh);
        query.setSkssqq(skssqq);
        query.setSkssqz(skssqz);
        Example<CitWgqyGrid> example = Example.of(query);
        return fb.findAll(example);
    }
}
